{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 作业1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 逐个更新权重，多次迭代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mxnet import nd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def stepFunction(x):\n",
    "    return 1 if x.asscalar() >0 else 0\n",
    "def getDataset():\n",
    "    xs=nd.array([[1,1],[1,0],[0,1],[0,0]])\n",
    "    ys=nd.array([1,0,0,0])\n",
    "    return xs,ys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Perceptron(object):\n",
    "    def __init__(self,activator):#初始化变量\n",
    "        self.activator=activator\n",
    "        self.weights=nd.array([])\n",
    "        self.bias=nd.zeros(1)\n",
    "    def __str__(self):#特殊函数，输出字符串\n",
    "        w=self.weights.asnumpy().tolist()\n",
    "        b=self.bias.asnumpy().tolist()\n",
    "        strw='weights:%s'%w\n",
    "        strb='bias:%s'%b\n",
    "        return strw+'\\n'+strb\n",
    "    def net(self,x):#计算输出值，预测值\n",
    "        result=self.activator(nd.dot(self.weights,x)+self.bias)\n",
    "        return result\n",
    "    def updateWeights(self,x,yhat,y,eta):#更新权重和偏差\n",
    "        deltaw=eta*(y-yhat)*x\n",
    "        self.weights+=deltaw\n",
    "\n",
    "        deltab=eta*(y-yhat)\n",
    "        self.bias+=deltab\n",
    "    def epoch(self,xs,ys,eta):#逐个计算预测值，然后更新权重\n",
    "        for x ,y in zip(xs,ys):\n",
    "            yhat=self.net(x)\n",
    "            self.updateWeights(x,yhat,y,eta)\n",
    "    def train(self,xs,ys,epochs,eta):#初始化权重，迭代更新权重\n",
    "        inputNum=len(xs[0])\n",
    "        self.weights=nd.zeros(inputNum)\n",
    "        for i in range(epochs):\n",
    "            self.epoch(xs,ys,eta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       " [[ 1.  1.]\n",
       "  [ 1.  0.]\n",
       "  [ 0.  1.]\n",
       "  [ 0.  0.]]\n",
       " <NDArray 4x2 @cpu(0)>, \n",
       " [ 1.  0.  0.  0.]\n",
       " <NDArray 4 @cpu(0)>)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs,ys=getDataset()\n",
    "xs,ys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "andP=Perceptron(stepFunction)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weights:[]\n",
      "bias:[0.0]\n"
     ]
    }
   ],
   "source": [
    "print(andP)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "andP.train(xs,ys,10,0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weights:[0.009999999776482582, 0.019999999552965164]\n",
      "bias:[-0.019999999552965164]\n"
     ]
    }
   ],
   "source": [
    "print(andP)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.0, 1.0] 1\n",
      "[1.0, 0.0] 0\n",
      "[0.0, 1.0] 0\n",
      "[0.0, 0.0] 0\n"
     ]
    }
   ],
   "source": [
    "for x in xs:\n",
    "    print(x.asnumpy().tolist(),andP.net(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x=nd.array([1,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2,)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 作业2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       " [[ 1.  1.]\n",
       "  [ 1.  0.]\n",
       "  [ 0.  1.]\n",
       "  [ 0.  0.]]\n",
       " <NDArray 4x2 @cpu(0)>, \n",
       " [ 1.  0.  0.  0.]\n",
       " <NDArray 4 @cpu(0)>)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs,ys=getDataset()\n",
    "xs,ys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[ 0.01  0.02]\n",
       "<NDArray 2 @cpu(0)>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w=andP.weights\n",
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[ 0.03  0.01  0.02  0.  ]\n",
       "<NDArray 4 @cpu(0)>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.dot(xs,w.T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 作业3\n",
    "- 数据输入集不变，输出集变为[1,1,1,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def ORgetDataset():\n",
    "    xs=nd.array([[1,1],[1,0],[0,1],[0,0]])\n",
    "    ys=nd.array([1,1,1,0])\n",
    "    return xs,ys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       " [[ 1.  1.]\n",
       "  [ 1.  0.]\n",
       "  [ 0.  1.]\n",
       "  [ 0.  0.]]\n",
       " <NDArray 4x2 @cpu(0)>, \n",
       " [ 1.  1.  1.  0.]\n",
       " <NDArray 4 @cpu(0)>)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs,ys=ORgetDataset()\n",
    "xs,ys "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "orP=Perceptron(stepFunction)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weights:[]\n",
      "bias:[0.0]\n"
     ]
    }
   ],
   "source": [
    "print(orP)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "orP.train(xs,ys,10,0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weights:[0.009999999776482582, 0.009999999776482582]\n",
      "bias:[0.0]\n"
     ]
    }
   ],
   "source": [
    "print(orP)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.0, 1.0] 1\n",
      "[1.0, 0.0] 1\n",
      "[0.0, 1.0] 1\n",
      "[0.0, 0.0] 0\n"
     ]
    }
   ],
   "source": [
    "for i in xs:\n",
    "    print(i.asnumpy().tolist(),orP.net(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
